package logic

import (
	"context"
	"doctor-medicine-system/model/mysql"
	"errors"

	"doctor-medicine-system/goods/goods"
	"doctor-medicine-system/goods/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *goods.LoginRequest) (*goods.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User

	if err := user.LoginUser(mysql.DB, in.WorkNumber); err != nil {
		return nil, err
	}
	if user.ID == 0 {
		return nil, errors.New("医生不存在")
	}
	if in.Password != user.Password {
		return nil, errors.New("密码不正确")
	}
	return &goods.LoginResponse{
		UserId: int64(user.ID),
	}, nil
}
